<template>
  <div class="app-container">
    <el-form ref="dataForm" :model="channelForm" label-position="left" label-width="140px">
      <el-form-item label="二维码名称" prop="code">
        <el-input v-model="channelForm.code" />
      </el-form-item>
      <el-form-item label="选择分组" prop="title">
          <el-select v-model="value" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="客服人员" prop="title">
        <el-radio v-model="radio" label="1">全天在线</el-radio>
        <el-radio v-model="radio" label="1">自动上下线</el-radio>
      </el-form-item>
      <el-form-item label="员工添加上限">
        <el-switch
          v-model="value"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="允许员工上下线">
        <el-switch
          v-model="value"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="备用员工" prop="title">
        <el-input v-model="channelForm.title" />
      </el-form-item>
      <el-form-item label="客户标签" prop="title">
        <el-switch
          v-model="value"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="客户备注" prop="title">
        <el-switch
          v-model="value"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="客户描述" prop="title">
        <el-switch
          v-model="value"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="设置欢迎语" prop="title">
        <el-radio v-model="radio" label="1">渠道欢迎语</el-radio>
        <el-radio v-model="radio" label="1">默认欢迎语</el-radio>
        <el-radio v-model="radio" label="1">不发送欢迎语</el-radio>
      </el-form-item>
      <el-form-item label="分时段欢迎语" prop="title">
        <el-switch
          v-model="value"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="欢迎语屏蔽" prop="title">
        <el-switch
          v-model="value"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="自动通过好友" prop="title">
        <el-switch
          v-model="value"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="开启时间" prop="title">
        <el-radio v-model="radio" label="1">全天候开启</el-radio>
        <el-radio v-model="radio" label="1">选择时间段</el-radio>
      </el-form-item>
      <el-form-item label="二维码预览" prop="title">
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="channelForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="Please input" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="channelForm.status.value === 5" type="danger" @click="handleDelete(channelForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveChannel, removeChannelById, getChannel } from '@/api/customer/channel'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Upload from '@/components/Upload/SingleImage' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      channelForm: {
        code: '',
        title: '',
        cover: '',
        remark: '',
        content: '',
        status: { label: '', value: 1 }
      },
      channelRules: []
    }
  },
  created() {
    let channelId = this.$route.query && this.$route.query.channelId
    if (channelId !== undefined) {
      channelId = parseInt(channelId)
      if (!isNaN(channelId)) {
        this.listLoading = true
        const formData = {
          'channelId': channelId
        }
        getChannel(formData).then(response => {
          if (response.status === 200) {
            this.channelForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(channelId)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'channel' })
    },
    createData() {
      this.listLoading = true

      let _channelForm = this.channelForm
      try {
        _channelForm.status = _channelForm.status.value
        if(_channelForm.status == undefined){
          _channelForm.status = 1
        }
      } catch (error) {
        _channelForm.status = 1
      }
      if (_channelForm.identity > 0) {
        _channelForm.channelId = _channelForm.identity
      }
      console.log(this.channelForm,_channelForm)
      saveChannel(_channelForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'channel' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      this.$confirm('确实要执行此操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        const formData = {
          'channelId': row.identity
        }
        removeChannelById(formData).then(response => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'group' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
